/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.qdu.dao.impl;

import com.qdu.dao.DianzanDao;
import com.qdu.entity.Dianzan;
import com.qdu.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Dianzan表数据库操作的实现代码
 * @author Lenovo
 */
public class DianzanDaoImpl extends BaseDaoImpl1 implements DianzanDao{

    @Override
    public boolean addOneLike(String jid, int uid) {
        if(executeUpdate("insert into Dianzan(Jid,id) values(?,?)"
                              ,jid
                              ,uid
        )>0)
            return true;
        return false;
    }
    
    @Override
    public boolean hasLike(String Jid,int uid){ 
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            con =DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Dianzan where Jid=? and id=?");
            ps.setString(1, Jid);
            ps.setInt(2, uid);
            rs = ps.executeQuery();
  
            while (rs.next()) {
                System.out.println("-----------已存在此点赞记录---------");
                System.out.println(rs.getString("Jid"));
                System.out.println(rs.getInt("id"));
                System.out.println("-----------------------------------");
                return true;
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return false;
    }

    @Override
    public int addLikeNum(String Jid)
    {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        
        try {
            con =DatabaseUtil.getConnection();
            ps = con.prepareStatement("update Journal set likenum=likenum+1 where Jid=?");
            ps.setString(1, Jid);
            if( ps.executeUpdate()>0 )
            {
                ps=con.prepareStatement("select likenum from Journal where Jid=?");
                ps.setString(1, Jid);
                rs=ps.executeQuery();
                while(rs.next())
                {
                    return rs.getInt(1);
                }
            }
           
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
        return 0;
    }
    
}
